//
//  DrawView.m
//  30207
//
//  Created by student on 16/4/7.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "DrawView.h"
#import "DrawLinePath.h"
@interface DrawView()
@property (nonatomic,strong) DrawLinePath *curpath;
@property (nonatomic,strong) NSMutableArray *patharr;
@end
@implementation DrawView
-(void)awakeFromNib
{
    self.curlinrwidth=1;
    self.curcolor=[UIColor blackColor];
}
-(NSMutableArray *)patharr
{
    if(_patharr==nil)
    {
        _patharr=[NSMutableArray array];
    }
    return _patharr;
}
-(void)undo
{
    [self.patharr removeLastObject];
    [self setNeedsDisplay];
}
-(void)clear
{
    [self.patharr removeAllObjects];
    [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
    for(DrawLinePath *path in self.patharr)
    {
        [path.color set];
        [path stroke];
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    CGPoint point=[touch locationInView:self];
    self.curpath=[DrawLinePath bezierPath];
    self.curpath.lineWidth=self.curlinrwidth;
    self.curpath.color=self.curcolor;
    [self.patharr addObject:self.curpath];
    [self.curpath moveToPoint:point];
    [self setNeedsDisplay];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    CGPoint point=[touch locationInView:self];
    [self.curpath addLineToPoint:point];
    [self setNeedsDisplay];
}
@end
